package com.qidian.tcba.admin.service.impl;


import com.qidian.tcba.admin.model.dto.attachment.AttachmentBaseInfoDTO;
import com.qidian.tcba.admin.model.dto.parkLot.ParkLotAttachmentsInfoDTO;
import com.qidian.tcba.admin.model.entity.Attachment;
import com.qidian.tcba.admin.service.AttachmentService;
import com.qidian.tcba.dao.AttachmentMapper;
import com.qidian.tcba.oss.model.dto.AttachmentResultDTO;
import com.qidian.tcba.utils.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qidian.tcba.utils.TablesName;

import static com.qidian.tcba.utils.BusinessType.*;
import static com.qidian.tcba.utils.TablesName.PARK_LOT_RECORD_TASK_NAME;

/**
 * <p>
 * 附件表 服务实现类
 * </p>
 *
 * @author 冯永龙
 * @since 2021-07-06
 */
@Service
public class AttachmentServiceImpl implements AttachmentService {

    @Autowired
    private AttachmentMapper attachmentMapper;

    public void handleMap(Map<Integer, List<AttachmentResultDTO>> map, int key, Attachment attachment) {
        map.computeIfAbsent(key, k -> new ArrayList<>());
        map.get(key).add(BeanMapper.map(attachment,AttachmentResultDTO.class));
    }

    @Override
    public ParkLotAttachmentsInfoDTO getParkLotAttachmentsInfo(int parkLotId) {
        List<Attachment> attachments = attachmentMapper.getAttachments(PARK_LOT_RECORD_TASK_NAME, parkLotId);

        Map<Integer, List<AttachmentResultDTO>> map = new HashMap<>();
        for (Attachment attachment : attachments) {
            int businessType = attachment.getBusinessType();
            switch (businessType) {
                case PROSPECT_FORM:
                    handleMap(map,PROSPECT_FORM,attachment);
                    break;
                case APPLICATION_RECORD_FORM:
                    handleMap(map,APPLICATION_RECORD_FORM,attachment);
                    break;
                case PROPERTY_RIGHT:
                    handleMap(map,PROPERTY_RIGHT,attachment);
                    break;
                case BUSINESS_LICENSE:
                    handleMap(map,BUSINESS_LICENSE,attachment);
                    break;
                case RECOMMENDATION:
                    handleMap(map,RECOMMENDATION,attachment);
                    break;
                case CONFIRMATION_OF_COMPLETION:
                    handleMap(map,CONFIRMATION_OF_COMPLETION,attachment);
                    break;
                case TRAFFIC_ORGANISATION_CHART:
                    handleMap(map,TRAFFIC_ORGANISATION_CHART,attachment);
                    break;
                case PARK_SETTING_LIST:
                    handleMap(map,PARK_SETTING_LIST,attachment);
                    break;
                case SERVICE_AND_SAFE_SYSTEM:
                    handleMap(map,SERVICE_AND_SAFE_SYSTEM,attachment);
                    break;
                case MANAGE_OPERATE_MAINTAIN_PROJECT :
                    handleMap(map,MANAGE_OPERATE_MAINTAIN_PROJECT,attachment);
                    break;
                case EMERGENCY_RESPONSE_PLAN:
                    handleMap(map,EMERGENCY_RESPONSE_PLAN,attachment);
                    break;
                case OTHERS :
                    handleMap(map,OTHERS,attachment);
                    break;
                case PERMISSION_RIGHT:
                    handleMap(map,PERMISSION_RIGHT,attachment);
                    break;
            }
        }
        return new ParkLotAttachmentsInfoDTO(
                map.get(PROSPECT_FORM),
                map.get(APPLICATION_RECORD_FORM),
                map.get(PROSPECT_FORM),
                map.get(BUSINESS_LICENSE),
                map.get(RECOMMENDATION),
                map.get(CONFIRMATION_OF_COMPLETION),
                map.get(TRAFFIC_ORGANISATION_CHART),
                map.get(PARK_SETTING_LIST),
                map.get(SERVICE_AND_SAFE_SYSTEM),
                map.get(MANAGE_OPERATE_MAINTAIN_PROJECT),
                map.get(EMERGENCY_RESPONSE_PLAN),
                map.get(OTHERS),
                map.get(PERMISSION_RIGHT));
    }
}
